﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using crm.model;

namespace crm.aplicacao.CRMSystem
{
    public partial class GLeads : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                carregarOrigemLeads();
                carregarLeads();
            }
        }

        [DirectMethod]
        public void inserir()
        {
            Leads obj = new Leads();
            obj.Nome = txtNomeLeads.Text;
            obj.Empresa = txtEmpresaLeads.Text;
            obj.Email = txtEmailLeads.Text;
            obj.Telefone = txtTelefoneLeads.Text;
            obj.FkIdOrigemLeads = Convert.ToInt32(cboOrigemLeads.SelectedItem.Value);
            obj.Observacao = txtObservacaoLeads.Text;

            
            LeadsDao objDao = new LeadsDao();

            var objValida = objDao.consultar(txtNomeLeads.Text);

            if (objValida.Rows.Count > 0)
                X.Msg.Alert("Status", "Este item já está cadastrado!").Show();
            else
                if (objDao.inserir(obj))
                    X.Msg.Alert("Status", "Inserido com sucesso!").Show();
                else
                    X.Msg.Alert("Status", "Não foi possível inserir!").Show();

            WindowsNovoLeads.Hide();
            carregarLeads();
        }

        void carregarOrigemLeads()
        {
            OrigemLeadsDao objDao = new OrigemLeadsDao();
            var dados = objDao.consultar();
            this.strOrigemLeads.DataSource = dados;
            this.strOrigemLeads.DataBind();
        }

        void carregarLeads()
        {
            LeadsDao objDao = new LeadsDao();
            var dados = objDao.consultar();
            this.strLeads.DataSource = dados;
            this.strLeads.DataBind();
        }

        [DirectMethod]
        public void limparCampo()
        {
            this.txtNomeLeads.Text = "";
            this.txtEmpresaLeads.Text = "";
            this.txtEmailLeads.Text = "";
            this.txtTelefoneLeads.Text = "";
            this.txtObservacaoLeads.Text = "";
            
        }
    }
}